// 加载中间件和全局定义
const express = require('express');
const cors = require('cors');
const userRoutes = require('./routes/userRoutes');
const surveyRouter = require('./routes/surveyRouter');
const responseRouter = require('./routes/responseRouter');

const app = express();

// 中间件
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 路由
app.use('/api/users', userRoutes);
app.use('/api/surveys', surveyRouter);
app.use('/api/responses', responseRouter);

// 错误处理中间件
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).json({
    code: 500,
    message: '服务器错误'
  });
});

module.exports = app;
